#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : Chestnut
# @time    : ${DATE} ${TIME}
# @function: tdms文件截取片段，
# @version : V1

from nptdms import TdmsFile
import os
import datetime


class Cuter:
    def __init__(self, resource_file, target_file):
        self.resource_file = resource_file
        self.target_file = target_file
        self.save_file_9235 = open(os.path.join(self.target_file, f"{os.path.basename(self.resource_file)}_9235.txt"),
                                   "w")
        self.save_file_9205 = open(os.path.join(self.target_file, f"{os.path.basename(self.resource_file)}_9205.txt"),
                                   "w")

    def read_tdms(self):
        with TdmsFile.read(self.resource_file) as tdms_file:
            group_9235 = tdms_file.groups()[0]
            channels_9235 = group_9235.channels()
            channel_9235 = channels_9235[0]
            data_lenth_9235 = channels_9235.data.size
            data_width_9235 = len(channels_9235)
            wf_start_time_9235 = (channel_9235.properties['wf_start_time'].astype(datetime.datetime))
            group_9205 = tdms_file["9205电压"]
            channels_9205 = group_9205.channels()
            channel_9205 = channels_9205[0]
            data_lenth_9205 = channels_9205.data.size
            data_width_9205 = len(channels_9205)
            wf_start_time_9205 = (channel_9205.properties['wf_start_time'].astype(datetime.datetime))

            return [
                self.resource_file,  # 文件名
                data_lenth_9235,  # 9235数据长度
                data_width_9235,  # 9235数据宽度
                wf_start_time_9235,  # 9235开始时间
                channel_9235.properties['wf_samples'],  # 9235采样率
                data_lenth_9205,  # 9205数据长度
                data_width_9205,  # 数据宽度
                wf_start_time_9205,  # 9205开始时间
                channel_9205.properties['wf_samples'],  # 9205采样率
            ]
